<?php


namespace app\models;


use app\event\MsgAfterEvent;
use app\event\MsgBeforeEvent;
use yii\base\Model;

/**
 * Notes: 信息发送事件处理类
 * User: aniu
 * DateTime: 2021/3/27 18:23
 * Class MsgHandler
 * @package app\models
 */
class MsgHandler extends Model
{
    /**
     * Notes:处理发送信息前的事件
     * User: aniu
     * DateTime: 2021/3/27 18:29
     * @param MsgBeforeEvent $event
     */
    public function beforeSendMsg(MsgBeforeEvent $event)
    {
        //从event中读取日期、和附加信息
        $logContent = "BEFORE: ".$event->date.', extraMsg: '.$event->data."\n";
        //写入文件中
        file_put_contents('log.txt',$logContent, FILE_APPEND);
    }


    /**
     * Notes:处理发送信息后的事件
     * User: aniu
     * DateTime: 2021/3/27 18:29
     */
    public function afterSendMsg(MsgAfterEvent $event)
    {
        //从event中读取数据
        $logContent = "AFTER: ".$event->from.'发送给'.$event->to.', 内容如下：'.$event->message."\n";
        file_put_contents('log.txt',$logContent,FILE_APPEND);
    }
}